import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'
Vue.use(VueRouter)

const routes = [
  {
    // 登录页面
    path:'/',
    name:'login',
    component:login
  },{
    // 首页
    path:'/Home',
    mame:'Home',
    component:()=>import('../views/Home.vue'),
    children: [
      // 子路由中的path前面不要加 /
      {
        // 用户列表
        path: '/Users',
        name: 'Users',  // 重定向:重新指向其它path,会改变网址
        component:()=>import('../views/Users.vue')
      },
      {
        // 角色列表
        path:'/Roles',
        name:'Roles',
        component:()=>import('../views/Roles.vue')
      },{
        // 权限列表
        path:'/Rights',
        name:'Rights',
        component:()=>import('../views/Rights.vue')
      },{
        // 商品列表
        path:'/Goods',
        name:'Goods',
        component:()=>import('../views/Goods.vue')
      },{
        // 分类参数
        path:'/Params',
        name:'Params',
        component:()=>import('../views/Params.vue')
      },{
        // 商品分类
        path:'/Categories',
        name:'Categories',
        component:()=>import('../views/Categories.vue')
      },{
        // 订单列表
        path:'/Orders',
        name:'Orders',
        component:()=>import('../views/Orders.vue')
      },{
        // 数据报表
        path:'/Reports',
        name:'Reports',
        component:()=>import('../views/Reports.vue')
      }
      
    ],
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 配置路由守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/'){
    next()
  }else{
    const token = localStorage.getItem('token')
    if(token){
      next()
    }else{
      next('/')
    }
  }
})

export default router
